import pygame.image

from game import SCREEN_WIDTH

"""
文本分割工具类
"""


def blit_text(surface, text, pos, font, color=pygame.Color("blue")):
    words = text.split(";")
    x, y = pos
    for line in words:
        word_surface = font.render(line, None, color)
        surface.blit(word_surface, (x, y))
        word_width, word_height = word_surface.get_size()
        y += word_height


"""
基类系统对话类
"""


class SysBaseDialog:
    def __init__(self, text_list: list, font):
        """
        参数初始化
        :param text_list:
        :param font:
        """
        self.text_list = text_list
        self.current_id = 0
        self.dialog_image = pygame.image.load("./resources/images/dialog/sys_dialog.png")
        x, y, w, h = self.dialog_image.get_rect()
        self.pos_x = int(SCREEN_WIDTH - w) / 2 + 35
        self.pos_y = 35
        self.finish = False
        self.font = font

    def draw(self, surface, view_x, view_y):
        """
        绘制对话内容
        :param surface:
        :param view_x:
        :param view_y:
        :return:
        """
        if self.current_id >= len(self.text_list):
            return
        else:
            surface.blit(self.dialog_image, (self.pos_x, self.pos_y))
            blit_text(surface, self.text_list[self.current_id],
                      (self.pos_x + 30, self.pos_y + 30), self.font)

    def is_finish(self):
        """
        是否完成
        :return:
        """
        return self.finish

    def next_text(self):
        """
        展示下一个对话
        :return:
        """
        self.current_id += 1
        if self.current_id >= len(self.text_list):
            self.finish = True
            self.current_id = len(self.text_list)


"""
人物基类对话类
"""


class ActorBaseDialog(SysBaseDialog):
    def __init__(self, actor, text_list: list, font):
        """
        初始化
        :param actor:
        :param text_list:
        :param font:
        """
        super().__init__(text_list, font)
        self.text_list = text_list
        self.current_id = 0
        self.dialog_image = pygame.image.load("./resources/images/dialog/actor_dialog.png")
        self.actor = actor
        self.finish = False
        self.font = font

    def draw(self, surface, view_x, view_y):
        """
        绘制
        :param surface:
        :param view_x:
        :param view_y:
        :return:
        """
        if self.current_id >= len(self.text_list):
            return
        temp_x = self.actor.pos_x - view_x + 5
        temp_y = self.actor.pos_y - view_y - 55
        surface.blit(self.dialog_image, (temp_x, temp_y))
        # 将文字绘制上去
        blit_text(surface, self.text_list[self.current_id],
                  (temp_x + 10, temp_y + 10), self.font)
